package com.cineplanet.dew.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cineplanet.dew.exception.DAOExcepcion;
import com.cineplanet.dew.modelo.Cine;
import com.cineplanet.dew.modelo.Pelicula;
import com.cineplanet.dew.service.CineService;
import com.cineplanet.dew.service.PeliculaService;

public class IndexController extends MultiActionController{
	
	private PeliculaService peliculaService;
	
	public PeliculaService getPeliculaService() {
		return peliculaService;
	}

	public void setPeliculaService(PeliculaService peliculaService) {
		this.peliculaService = peliculaService;
	}

	private CineService cineService;
	
	public CineService getCineService() {
		return cineService;
	}

	public void setCineService(CineService cineService) {
		this.cineService = cineService;
	}

	public ModelAndView index(HttpServletRequest request,HttpServletResponse response)  {
		
		try {
			List<Pelicula> peliculas = peliculaService.listarEnCartelera();	
			List<Pelicula> peliculas2 = peliculaService.listarProximoEstreno();	
			
			List<Cine> cines = cineService.listar();	
			
			request.setAttribute("cartelera", peliculas);
			request.setAttribute("proximos", peliculas2);
			request.setAttribute("cines", cines);
			
		} catch (DAOExcepcion e) {
			e.printStackTrace();
		}
		
		
       return new ModelAndView("index");
    }

	public ModelAndView mostrarPeliculasPorCine(HttpServletRequest request, HttpServletResponse response) {
		
        int idCine = Integer.parseInt(request.getParameter("idCine"));
       
        try {
        	List<Pelicula> peliculas = (ArrayList<Pelicula>)peliculaService.listarPeliculasPorCine(idCine);	
        	PrintWriter out = response.getWriter();
        	
        	out.println("<select name=\"cboPeliculas\" >");
        	out.println("<option value=\"0\">Seleccione</option>");
        	for(Pelicula pela : peliculas){
        		out.println("<option value=\"" + pela.getIdPelicula() + "\" onclick=\"window.location='detallePelicula.htm?idPelicula=" + pela.getIdPelicula() + "'\">"+ pela.getNombre()+ "</option>");
        	}
        	out.println("</select>");

        } catch (DAOExcepcion e) {
            System.err.println("Error");
        } catch (IOException e) {
			e.printStackTrace();
		}
        
        return null;
    }
	
	

}
